package com.ebc.eservice.util;

import org.springframework.stereotype.Component;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author: Joey
 * @Description:时间工具
 * @date:2022/9/27 16:07
 */
@Component
public class MyTimeUtil {

    //获取今天日期
    public String getToDay(){
        long today = System.currentTimeMillis();
        Timestamp day = new Timestamp(today); //(i * 24*60*60*1000)
        //        System.out.println(times);
        String time = day.toString().split("\\s")[0].substring(0,10);
        return time;
    }

    //获取近7天日期
    public List<String> getSevenDay(){
        List<String> list = new ArrayList<>();
        long today = System.currentTimeMillis();
        for (int i = 0; i < 7; i++) {
            Timestamp day = new Timestamp(today - (i * 24*60*60*1000));
            String times = day.toString().split("\\s")[0].substring(0,10);// 2022 -09-12
            list.add(times);
        }
        System.out.println("最近7天 => "+list);
        return list;
    }
    public List<String> getSevenDayExYear(){
        List<String> list2 = new ArrayList<>();
        long today = System.currentTimeMillis();
        for (int i = 0; i < 7; i++) {
            Timestamp day = new Timestamp(today - (i * 24*60*60*1000));
            String times = day.toString().split("\\s")[0].substring(5,10);// 09-12
            list2.add(times);
        }
        System.out.println("最近7天 => "+list2);
        return list2;
    }

    //日期转换为String yy-MM-dd
    public String FormatTime(Date date){
        long dateTime = date.getTime();
        Timestamp day = new Timestamp(dateTime);
        return day.toString().split("\\s")[0].substring(0,10);
    }

    public String toDateString(Date date){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sf.format(date);
    }



    //2个日期转换
    public List<String> getTwoTime(Date one,Date two){
        String time = FormatTime(one);
        String time2 = FormatTime(two);
        List<String> list = new ArrayList<>();
        list.add(time);
        list.add(time2);
        return list;
    }
    //日期差 天数
    public int getDateDist(Date begin,Date end){
        long d1 = begin.getTime();//起始
        long d2 = end.getTime();//结束
//        Timestamp day1 = new Timestamp(dateTimeOne);
//        Timestamp day2 = new Timestamp(dateTimeTwo);
        return (int) ((d2 - d1)/(1000*60*60*24));
    }
    //获取日期列表
    public List<String> getAppointDateList(Date begin,int dateNum){
        long endDate = begin.getTime();
        List<String> list= new ArrayList<>();;
        String times=null;
        Timestamp oldday = null;
        for (int i = 0; i < dateNum; i++) {
            oldday = new Timestamp(endDate - (i * 24*60*60*1000));
            times = oldday.toString().split("\\s")[0];// 09-12
            list.add(times);
        }
        System.out.println(list);
      return list;
    }

    //获取60天前 Date 的日期
    public Date SixtyDayBefore() throws ParseException {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(new Date());
        rightNow.add(Calendar.MONTH,-1);//减少1个月
        rightNow.add(Calendar.DATE,+1);
        Date d2 = rightNow.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String s = format.format(d2);
        return format.parse(s);
    }
    public List<String> SixtyBefore(){
        Calendar begin = Calendar.getInstance();// 得到一个Calendar的实例
        begin.setTime(new Date()); // 设置时间为当前时间
        begin.add(Calendar.MONTH, -1);// 月份减1
        begin.add(Calendar.DATE, +1);// 日期加1
        Date result = begin.getTime();
        Calendar end = Calendar.getInstance();
        Long startTime = begin.getTimeInMillis();
        Long endTime = end.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24l;// 一天的时间转化为ms
        List<String> dates = new ArrayList<>();
        Long time = startTime;
        int i = 0;
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            dates.add(i, df.format(d));
            i++;
            time += oneDay;
        }
        //倒序
        Collections.reverse(dates);
//        System.out.println(dates);
        return dates;
    }

    public List<String> getSixMonth(){
        String dateString;
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        List<String> rqList = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            dateString = sdf.format(cal.getTime());
            rqList.add(dateString.substring(0, 7));
            cal.add(Calendar.MONTH, -1);
        }
        return rqList;
    }


    public static void main(String[] args) throws ParseException {
        MyTimeUtil timeUtil = new MyTimeUtil();
        System.out.println(timeUtil.getSixMonth());
    }



}
